package com.nit.dto.utils;

import net.sf.json.JSONObject;

public class TableRequestDTO {
    private Integer pageSize;
    private Integer currentPage;
    private String search;

    public TableRequestDTO() {
        this.currentPage = 1;
        this.pageSize = 10;
        this.currentPage = 1;
    }

    public TableRequestDTO(Integer pageSize, Integer currentPage, String search) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.search = search;
    }

    // 处理表格分页查询情况
    public JSONObject getJsonSearch() {
        if (this.search == null || this.search.equals("") || this.search.equals("null") || this.search.equals("{}")) {
            return new JSONObject();
        }
        return JSONObject.fromObject(this.search);
    }

    // 驼峰命名转下划线命名
    public String CamelCaseToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;
        if (!para.contains("_")) {
            for (int i = 0; i < para.length(); i++) {
                if (Character.isUpperCase(para.charAt(i))) {
                    sb.insert(i + temp, "_");
                    temp += 1;
                }
            }
        }
        return sb.toString().toLowerCase();
    }

    //得到分页起始条数
    public Integer getStart(Long count) {
        Integer tempPage = getCurrentPage();
        if ((tempPage - 1) * this.pageSize == count && tempPage != 1) {
            tempPage--;
            this.currentPage = tempPage;
        }
        return (tempPage - 1) * this.pageSize;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }
}
